package com.itc.bi.handler.dataSet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itc.bi.dto.DataSetCreateDto;
import com.itc.bi.entity.*;
import com.itc.bi.mapper.DataSynrecordMapper;
import com.itc.bi.service.XxlJobInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
public abstract class DataSet {

    public abstract List<DataSetColumnEntity> column(DirectDataSetEntity dataSet);
    public abstract void create(DataSetCreateDto dto);

    //数据集预览数据
    public List<Object> previewData(DataSetCreateDto dto){return null;}
    //根据数据集ID预览数据
    public List<Object> previewData(DirectDataSetEntity dataSet,Integer pageIndex,Integer pageSize){return null;}

    //数据集列表-直连
    public List<Object> list(DirectDataSetEntity dataSet){return new ArrayList<>();}

    //数据集分页-直连
    public IPage page(DirectDataSetEntity dataSet, Integer pageIndex, Integer pageSize){return new Page();}

    public void job(XxlJobInfoService xxlJobInfoService,DataSynrecordMapper mapper, DataSetCreateDto dto,String xxlHandler){
        DataSynrecordEntity syn = mapper.selectOne(Wrappers.lambdaQuery(DataSynrecordEntity.class).eq(DataSynrecordEntity::getDtId, dto.getId()));
        String cron = dto.getCron();
        if(StringUtils.isBlank(cron)){
            if(null != syn){
                xxlJobInfoService.stop(syn.getTaskId());
                xxlJobInfoService.removeJob(syn.getTaskId());
                mapper.deleteById(syn.getId());
            }
        }else{
            XxlJobInfoEntity xxlJob = new XxlJobInfoEntity();
            if(null == syn){
                xxlJob.setJobDesc("数据集SQL调度任务"+dto.getName());
                xxlJob.setScheduleConf(dto.getCron());
                xxlJob.setExecutorHandler(xxlHandler);
                JSONObject executorParam = new JSONObject();
                executorParam.put("dtId",dto.getId());
                xxlJob.setExecutorParam(JSON.toJSONString(executorParam));
                xxlJobInfoService.addJob(xxlJob);
                syn = new DataSynrecordEntity();
                syn.setId(IdWorker.getId());
                syn.setTableId(1L);
                syn.setDirId(1L);
                syn.setDtId(1L);
                syn.setTaskId(xxlJob.getId());
                syn.setTargetTableName("SQL");
                syn.setTargetWornTable("SQL");
                syn.setCron(cron);
                syn.setSynType(1);
                syn.setSynWhere("sql");
                syn.setExecNumber(0);
                syn.setIsExec(1);
                mapper.insert(syn);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(5000);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        xxlJobInfoService.start(xxlJob.getId());
                    }
                }).start();
            }else{
                syn.setCron(cron);
                mapper.updateById(syn);
                xxlJob.setId(syn.getTaskId());
                xxlJob.setJobDesc("数据集SQL调度任务"+dto.getName());
                xxlJob.setScheduleConf(dto.getCron());
                xxlJob.setExecutorHandler(xxlHandler);
                JSONObject executorParam = new JSONObject();
                executorParam.put("dtId",dto.getId());
                xxlJob.setExecutorParam(JSON.toJSONString(executorParam));
                xxlJobInfoService.addJob(xxlJob);

                XxlJobInfoEntity job = xxlJobInfoService.getById(syn.getTaskId());
                if(1 == job.getTriggerStatus()){
                    xxlJobInfoService.stop(syn.getTaskId());
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                Thread.sleep(5000);
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }
                            xxlJobInfoService.start(xxlJob.getId());
                        }
                    }).start();
                }
            }
        }
    }
}
